$(function(){
	var userGrid = $("#payRecordDetailGrid");
	var ids=document.getElementById("detailId").value;
	var AllMoney = $("#AllMoney").val();

	userGrid.datagrid({
		url:'/payrecord/allDetail/'+ ids,
		fit:true,
		border:false,
		rownumbers:true,
		fitColumns:true,
		emptyMsg:"<img src='public/images/noxq.png'>",
		pagination : true,
		columns : [ [ {
			field : 'id',
			title : 'id',
			width : 100,
			align: 'center',
			hidden:true
		}, {
		
			field : 'money',
			title : '本次缴费金额',
			width : 100,
			align: 'center'
		}, {
			field : 'payWay',
			title : '缴费方式',
			width : 80,
			align: 'center'
		}, {
			
			field : 'payType',
			title : '缴费类型',
			align: 'center',
			width : 100
		}, {
			field : 'payTime',
			title : '缴费时间',
			align: 'center',
			width : 100
			
		}, {
			field : 'payrecord',
			title : 'id',
			align: 'center',
			width : 100,
			formatter: function (value, rec) {
				return rec.payrecord.id;
		     },
		    hidden:true
		}, {
			field : 'rec.image.path',
			title : '凭据',
			align: 'center',
			width : 100,
			formatter: function (value, rec, row, index) {
				
				var bts = [];
				bts.push('<a data-id="'+rec.image.id+'" data-index="'+index+'"  target=true  class="imgyl" href="payrecord/ylimg/'+rec.image.path+'">预览</a>');
				bts.push('<img id="yl'+rec.image.id+'" style="width:255px;height:175px; display: none;position: absolute;top: 80px;right:100px;" src="payrecord/ylimg/'+rec.image.path+'" />');
				return bts.join('');
		     }
		}, {
			field : 'emp',
			title : '员工',
			align: 'center',
			width : 100,
			formatter: function (value, rec) {
				return rec.emp.name;
		     }
		
		}, {
			field : 'ac',
			title : '操作',
			width : 80,
			align : 'center',
			formatter:function(value,row,index){
				var bts = [];		
				bts.push('<a data-index="'+index+'" class="ctr edit" >编辑</a>');
				/*bts.push('<a data-id="'+row.id+'" data-index="'+index+'" class="ctr delete" >删除</a>');*/
				return bts.join('');
			}
		} ] ],
		toolbar:[
		     {
		    	 iconCls:'icon-add',
		    	 text:"录入收款详情",
		    	 url:"add_payrecord_detail",
		    	 handler : function(index,rec,value) {
		    		 	detailForm();
				}
		     },   {
		    	 iconCls:'icon-back',
		    	 text:"返回",
		    	 url:"add_payrecord_detail",
		    	 handler : function(index) {
		    		 var tab=$("#tabs").tabs('getSelected');;
		 			var tabs=$("#tabs");
		 			var content = '<div scrolling="auto" frameborder="0"  style="width:100%;height:100%;"></div>';     
		 			tabs.tabs('update',{   
		 			  				tab:tab,
		 							options: {
		 								closable:true,
		 								title: '收款管理',
		 								content:content, 
		 								 href:'/payrecord' // the new content URL
		 							}
		                   });
		 		   tab.panel('refresh', '/payrecord');
				}
		     }
		]	    
	});
	
	

	//操作
	var gridPanel = userGrid.datagrid("getPanel");
	gridPanel.on("click","a.edit",function(){
		
		var index = this.dataset.index;
		// 如果只有下标，没有id的情况，要获取id
		var rows = userGrid.datagrid("getRows");
	
		// 同下标获取对应行的数据对象
		var id = rows[index].id;
		detailForm(id);
	}).on("click","a.delete",function(){
		var index = this.dataset.index;
		var id = this.dataset.id;
		alert(id);
		$.messager.confirm("提示", "您确定要删除此数据吗？", function(r) {
			if(r) {
				$.get("payrecord/deleteDetail/" + id, function(rs) {
					if (rs.success) {
						// 刷新表格
						userGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	}).on("mouseenter", "a.imgyl", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		var yl = $("#yl"+id);
		yl.css('display','block'); 
	}).on("mouseout", "a.imgyl", function() {
		var index = this.dataset.index;
		var id = this.dataset.id;
		var yl = $("#yl"+id);
		yl.css('display','none'); 
	});
	

	function detailForm(id) {
		var ids=document.getElementById("detailId").value;
		var pay=$("#pay").val();
		
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href : "payrecord/add_payrecord_detail"+ (id ? ("/" + id) : "")+(ids ? ("/" + ids) : ""),
			title :"录入收款详情",
			height : 340,
			width : 800,
			modal : true,
			onClose : function() {
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					var money = $("#money").val();
					if(parseInt(money)<parseInt(AllMoney)){
						$("#detailForm").form('submit', {
	                        url: 'payrecord/detail_save',
	                        data: $("#detailForm").serialize(),success:function(success) {
	                        	if(success){
	                        		//刷新数据表格
	            					userGrid.datagrid("reload");
	            					//关闭窗口
	    							dialog.dialog("close");
	                        	}else{
	                        		$.messager.alert("提示", "404");
	                        	}
	                        }
	                    });	
					}else{
						$.messager.alert("输入有误","亲，本次缴费金额不能大于总金额哦！");
					}
				}
			}]
			
		});
	}
});